package org.amf.helper;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * La classe NicInspector esegue il
 * Network Interface Card inspection
 *
 * recupera MACaddress, IPv4address
 *
 * @author Federico Casani
 */
public class NicInspector {

    private String MACaddress = "NA";
    private String IPv4address = "NA";

    public NicInspector() {

        this.doInspect();

    }

    private String doInspect() {

        try {

            Enumeration<NetworkInterface> niEnumeration = NetworkInterface.getNetworkInterfaces();
            while (niEnumeration.hasMoreElements()) {

                NetworkInterface networkInterface = niEnumeration.nextElement();

                // considero solo le NIC up
                if (!networkInterface.isUp() || networkInterface.isLoopback()) continue;

                /*
                 * MAC inspection
                 */
                this.doMACaddressInspect(networkInterface);
                /*
                 * MAC inspection
                 */
                this.doIPv4addressInspect(networkInterface);
                
            }

        } catch (Exception e) {
            
        }

        return MACaddress;
    }


    private void doMACaddressInspect(NetworkInterface networkInterface) {

        try {

            byte[] byteMAC = networkInterface.getHardwareAddress();

            StringBuffer MACBuffer = null;

            if (byteMAC != null) {
                MACBuffer = new StringBuffer();

                for (byte b : byteMAC) {
                    if (MACBuffer.length() > 0) {
                        MACBuffer.append(":");
                    }
                    
                    MACBuffer.append(Integer.toHexString((b >> 4) & 0x0f).toUpperCase());
                    MACBuffer.append(Integer.toHexString(b & 0x0f).toUpperCase());
                }

                MACaddress = MACBuffer.toString();
            }
        } catch (Exception e) {
            
        }

    }

    private void doIPv4addressInspect(NetworkInterface networkInterface) {

        try {

            Enumeration<InetAddress> iaEnumeration = networkInterface.getInetAddresses();

            while (iaEnumeration.hasMoreElements()) {

                InetAddress inetAddress = iaEnumeration.nextElement();

                if (inetAddress.isLoopbackAddress()) continue;

                if (inetAddress instanceof Inet4Address) {
                    IPv4address = inetAddress.getHostAddress();
                }

            }
        } catch (Exception e) {
            
        }

    }

    public String getIPv4address() {
        return IPv4address;
    }

    public void setIPv4address(String IPv4address) {
        this.IPv4address = IPv4address;
    }

    public String getMACaddress() {
        return MACaddress;
    }

    public void setMACaddress(String MACaddress) {
        this.MACaddress = MACaddress;
    }

    



}
